package springboot.j2024_11_3;


import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@RestController
@RequestMapping("/cookie")
public class CookieController {
    @RequestMapping("/get")
    public String getCookie(HttpServletRequest request ){
         //获取Cookie
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            Arrays.stream(cookies).forEach( x-> System.out.println("name:"+x.getName() +" value"+x.getValue()));
            return "cookie获取成功";
        }
        // 打印Cookie的name和value;
        // 上面用Lambda表达式是和for循环等价
//        for(Cookie x : cookies){
//            System.out.println("name:"+x.getName() +" value"+x.getValue());
//        }
        return "cookie为空";
    }
    @RequestMapping("/get2")
    public String getCookie2(@CookieValue("c1") String str){
         return "c1:"+str ;
    }

}
